package com.teamone.mobilewatch;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

import com.teamone.mobilewatch.util.UserSettings;

/**
 * 
 * Main menu
 * 
 */
public class MainMenuActivity extends Activity {
	private static final String TAG = "MainMenuActivity";
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// setup button click listeners
		ButtonHandler buttonHandler = new ButtonHandler();
		View btnWatchlist = findViewById(R.id.btnWatchlist);
		btnWatchlist.setOnClickListener(buttonHandler);

		View btnEdit = findViewById(R.id.btnEdit);
		btnEdit.setOnClickListener(buttonHandler);

		View btnDetails = findViewById(R.id.btnDetails);
		btnDetails.setOnClickListener(buttonHandler);

		View btnSettings = findViewById(R.id.btnSettings);
		btnSettings.setOnClickListener(buttonHandler);

	}
	@Override
	protected void onResume() {
		super.onResume();
		// load user settings
		UserSettings.load(this);
		// check if data is loaded
		if (!UserSettings.isLoaded()) {
			new AlertDialog.Builder(MainMenuActivity.this)
			.setMessage(
					"Please go to Settings and review preferences to be saved on the device")
			.setTitle("Setup")
			.setNeutralButton(android.R.string.ok,
					new DialogInterface.OnClickListener() {
						public void onClick(
								DialogInterface dialog,
								int whichButton) {
						}
					}).show();

		}
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case R.id.edit:
			startActivity(new Intent(this, EditActivity.class));
			return true;

		case R.id.details:
			startActivity(new Intent(this, DetailsActivity.class));
			return true;

		case R.id.settings:
			startActivity(new Intent(this, SettingsPreferenceActivity.class));
			return true;

		case R.id.admin:
			startActivity(new Intent(this, AdminActivity.class));
			return true;

		default:
			break;
		}

		return false;
	}



	/**
	 * 
	 * Class to handle button click events
	 * 
	 */
	class ButtonHandler implements OnClickListener {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btnWatchlist:
				startActivity(new Intent(getBaseContext(),
						PatientWatchActivity.class));
				break;
			case R.id.btnEdit:
				startActivity(new Intent(getBaseContext(), EditActivity.class));
				break;
			case R.id.btnDetails:
				startActivity(new Intent(getBaseContext(),
						DetailsActivity.class));
				break;
			case R.id.btnSettings:
				startActivity(new Intent(getBaseContext(),
						SettingsPreferenceActivity.class));
				break;
			}
		}

	}

}
